// Unit test for common.h objects

#include "geometry.h"
#include "unitTest.h"

using namespace simple;

namespace simple_test{

    UTFUNC(Point)
    {
        setDescription ("Point tests");

        Point p0;
        VERIFY (p0.x() == 0);
        VERIFY (p0.y() == 0);

        Point p1 (2,3);
        VERIFY (p1.x() == 2);
        VERIFY (p1.y() == 3);

        Point p2 = p1;
        VERIFY (p2.x() == 2);
        VERIFY (p2.y() == 3);
        VERIFY (p1 == p2);

        std::pair<int, int> pair = p2.point ();
        VERIFY (pair.first == 2);
        VERIFY (pair.second == 3);
    }



    UTFUNC(Rect)
    {
        setDescription ("Rect tests");

        Rect r0;
        VERIFY (r0.width() == 0);
        VERIFY (r0.height() == 0);
        VERIFY (r0.isNull ());

        Point p1 (1,2);
        Point p2 (3,5);

        // These 3 should be identical
        Rect r1 (p1, 2, 3);
        Rect r2 (p1, p2);
        Rect r3 (1, 2, 2, 3);
        VERIFY (r1 == r2);
        VERIFY (r1 == r3);
        VERIFY (r2 == r3);

        VERIFY (!r1.isNull ());
        VERIFY (r1.width() == 2);
        VERIFY (r1.height() == 3);
        VERIFY (r1.ul() == p1);
        VERIFY (r1.lr() == p2);

        VERIFY (r1.within (p1));
        VERIFY (!r1.within (p2));

        // Try all the points in the rectangle
        VERIFY (r1.within (Point (1,2)));
        VERIFY (r1.within (Point (1,3)));
        VERIFY (r1.within (Point (1,4)));
        VERIFY (r1.within (Point (2,2)));
        VERIFY (r1.within (Point (2,3)));
        VERIFY (r1.within (Point (2,4)));

        // Try some outside
        VERIFY (!r1.within (Point (0,2)));
        VERIFY (!r1.within (Point (1,1)));
        VERIFY (!r1.within (Point (2,1)));
        VERIFY (!r1.within (Point (3,2)));
        VERIFY (!r1.within (Point (3,4)));
        VERIFY (!r1.within (Point (2,5)));
        VERIFY (!r1.within (Point (1,5)));
        VERIFY (!r1.within (Point (0,4)));

        // Intersection
        Rect big (0, 0, 100, 200);
        Rect small1 (-1, -2, 2, 3);
        Rect small2 (90, -1, 20, 30);
        Rect small3 (90, 190, 20, 30);
        Rect small4 (-1, 190, 10, 20);

        // Partial overlap
        Rect int1 = big.intersect (small1);
        Rect int2 = big.intersect (small2);
        Rect int3 = big.intersect (small3);
        Rect int4 = big.intersect (small4);
        VERIFY (int1 == Rect (0, 0, 1, 1));
        VERIFY (int2 == Rect (90, 0, 10, 29));
        VERIFY (int3 == Rect (90, 190, 10, 10));
        VERIFY (int4 == Rect (0, 190, 9, 10));

        // Complete overlap
        Rect small5 (90, 100, 10, 20);
        Rect int5 = big.intersect (small5);
        VERIFY (int5 == small5);

        // No overlap
        Rect small6 (100, 200, 1, 1);
        Rect int6 = big.intersect (small6);
        VERIFY (int6.isNull ());
    }

}
